﻿using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;

namespace pinche_api.Controllers {
    /// <summary>
    /// API 基类
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    public class BaseController:ControllerBase{
        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="fileName">文件名称，如:1.jpg</param>
        /// <returns>HttpResponseMessage</returns>
        [HttpGet]
        public HttpResponseMessage GetImage(string fileName) {
            var imgPath = Directory.GetCurrentDirectory()+"/images/"+fileName;
            if(!System.IO.File.Exists(imgPath)) return new HttpResponseMessage(HttpStatusCode.NotFound);
            var imgStream = new MemoryStream(System.IO.File.ReadAllBytes(imgPath));
            var resp = new HttpResponseMessage(HttpStatusCode.OK) {
                Content=new StreamContent(imgStream)
            };
            resp.Content.Headers.ContentType=new MediaTypeHeaderValue("image/jpg");
            return resp;
        }
    }
}
